﻿using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using _2112A_2_MES.Data;
using Volo.Abp.DependencyInjection;

namespace _2112A_2_MES.EntityFrameworkCore;

public class EntityFrameworkCore_2112A_2_MESDbSchemaMigrator
    : I_2112A_2_MESDbSchemaMigrator, ITransientDependency
{
    private readonly IServiceProvider _serviceProvider;

    public EntityFrameworkCore_2112A_2_MESDbSchemaMigrator(
        IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public async Task MigrateAsync()
    {
        /* We intentionally resolve the _2112A_2_MESDbContext
         * from IServiceProvider (instead of directly injecting it)
         * to properly get the connection string of the current tenant in the
         * current scope.
         */

        await _serviceProvider
            .GetRequiredService<_2112A_2_MESDbContext>()
            .Database
            .MigrateAsync();
    }
}
